<?php
class Macys_Oauth_Oauth2  {

	private $log;

	// var to store the authentication instance
	// Defaults to code authentication
	private $authInstance;

	// var to store config info
	private $_config;


	private $_response;

	private $allowed_auth_types = array('mobile', 'code', 'token');

	public function __construct($config=null) {
		$this->log = Zend_Registry::get('log');
		if($config == null) {
			//$conf = new Macys_Base_Session();
			$conf = Macys_Base_Config::getInstance();
			$this->_config = $conf->getOAuthDetails();
		}
		else {
			$this->_config = $config;
		}
	}

	/**
	 * Browser response params are passed
	 * Depending on the type of Flow params will be verified
	 * @param Array $response
	 */
	public function setResponse($response){
		$this->_response = $response;
		return $this;
	}
	// Used from User Class
	public function setUserCred($username, $password) {
		$this->_response['username'] = $username;
		$this->_response['password'] = $password;
		return $this;
	}


	// performs the Authentications for Macys & Bloomingdales Authrization servers
	public function doAuthentication($type='code') {
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()) {
			return 'User Already Authenticated';
		}
		$result = false;
		if(in_array($type, $this->allowed_auth_types)){
			switch ($type) {
				case 'mobile':
					$this->authInstance = new Macys_Oauth_Authenticate_Mobile($this->_config);
					$this->authInstance->setAuth($this->_response['username'], $this->_response['password']);
					$result = $auth->authenticate($this->authInstance);
					$this->_checkAuthenticationResult($result);
					break;

				case 'code':
					$this->authInstance = new Macys_Oauth_Authenticate_Code($this->_config);
					if(isset($this->_response['code'])) {
						$this->authInstance->setCode($this->_response['code']);
						$result  = $auth->authenticate($this->authInstance);
						$this->_checkAuthenticationResult($result);
					} else {
						$this->authInstance->redirect();
					}
					break;

				case 'token':
					$this->authInstance = new Macys_Oauth_Authenticate_Token($this->_config);
					if(isset($this->_response['access_token'])) {
						$this->authInstance->setResponseParams($this->_response);
						$result  = $auth->authenticate($this->authInstance);
						$this->_checkAuthenticationResult($result);
					} else {
						$this->authInstance->redirect();
					}
					break;

				default:
					throw new Macys_Oauth_Exception('Authentication Type not set, valid types are mobile , code, token');
					break;
			}
		}
		else {
			throw new Macys_Oauth_Exception('Authentication Type not set, valid types are mobile , code, token');
		}
		return $result;
	}


	public function doLogout($type='code') {

		switch ($type) {
			case 'code':
				$this->authInstance = new Macys_Oauth_Authenticate_Code($this->_config);
				$this->authInstance->setRedirectUri('http://'. $_SERVER['HTTP_HOST']);
				break;
			case 'token':
				$this->authInstance = new Macys_Oauth_Authenticate_Token($this->_config);
				$this->authInstance->setRedirectUri('http://'. $_SERVER['HTTP_HOST']);
				break;
			default:
				// If incorrectly specified we will use Mobile Logout
				$this->authInstance = new Macys_Oauth_Authenticate_Mobile($this->_config);
				break;
		};
		$this->authInstance->doLogout();
		return $this;
	}

	public function refreshToken() {
		$this->authInstance = new Macys_Oauth_Authenticate_Code($this->_config);
		// TODO Save Refresh tokenfirst and then retrive here use class to perform refresh token
	}


	private function _checkAuthenticationResult($result) {
		if ($result->isValid()) {
			$auth = Zend_Auth::getInstance();
			$access_token = $auth->getIdentity();
			// do your stuff with access token
		} else {
			$this->log->crit("OAuth Code not recieved " . $result->getMessages());
		}
	}


}